package com.ruoyi.web.controller.system;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.config.ApiConfiguration;
import com.ruoyi.system.service.publics.data.PublicDataApiService;
import com.ruoyi.system.service.publics.data.impl.PublicDataApiServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api/system")
public class PublicDataApiController {

	private PublicDataApiService publicDataApiService = null;

	{
		ApiConfiguration config = new ApiConfiguration();
		config.setEndpoint("https://aws.okx.com/");
		// apiKey，api注册成功后页面上有
		config.setApiKey("61b65630-2472-4c94-baa2-5107669f1c19");
		config.setSecretKey("56638BE7E0C45E12585385666F53B552");
		config.setPassphrase("$*9eYgH$Ah2$!ww");
		config.setRetryOnConnectionFailure(true);
		this.publicDataApiService = new PublicDataApiServiceImpl(config);
	}

	/**
	 * 获取交易产品基础信息 Get Instruments GET /api/v5/public/instruments
	 */
	@GetMapping("/instruments")
	@ResponseBody
	public AjaxResult getInstruments() {

		JSONObject result = publicDataApiService.getInstruments("SWAP", null, "ETH-USDT-SWAP");
		return AjaxResult.success(result);

	}

	/**
	 * 获取交割和行权记录 Get Delivery/Exercise History GET
	 * /api/v5/public/delivery-exercise-history
	 */
	@GetMapping("/deliveryExeHis")
	@ResponseBody
	public AjaxResult getDeliveryExerciseHistory() {
		JSONObject result = publicDataApiService.getDeliveryExerciseHistory("OPTION", "BTC-USD", "", "", "");
		return AjaxResult.success(result);
	}

	/**
	 * 获取持仓总量 Get Open Interest GET /api/v5/public/open-interest
	 */
	@GetMapping("/openInterest")
	@ResponseBody
	public AjaxResult getOpenInterest() {
		JSONObject result = publicDataApiService.getOpenInterest("FUTURES", "LTC-USDT", "LTC-USDT-210416");
		return AjaxResult.success(result);
	}

	/**
	 * 获取永续合约当前资金费率 Get Funding Rate GET /api/v5/public/funding-rate
	 */
	@GetMapping("/fundingRate")
	@ResponseBody
	public AjaxResult getFundingRate() {
		JSONObject result = publicDataApiService.getFundingRate("BTC-USDT-SWAP");
		return AjaxResult.success(result);
	}

	/**
	 * 获取永续合约历史资金费率 Get Funding Rate History GET /api/v5/public/funding-rate-history
	 */
	@GetMapping("/fundingRateHistory")
	@ResponseBody
	public AjaxResult getFundingRateHistory() {
		JSONObject result = publicDataApiService.getFundingRateHistory("ETH-USD-SWAP", "", "", "");
		return AjaxResult.success(result);
	}

	/**
	 * 获取限价 Get Limit Price GET /api/v5/public/price-limit
	 */
	@GetMapping("/limitPrice")
	@ResponseBody
	public AjaxResult getLimitPrice() {
		JSONObject result = publicDataApiService.getLimitPrice("BTC-USD-210326");
		return AjaxResult.success(result);
	}

	/**
	 * 获取期权定价 Get Option Market Data GET /api/v5/public/opt-summary
	 */
	@GetMapping("/optionMarketData")
	@ResponseBody
	public AjaxResult getOptionMarketData() {
		JSONObject result = publicDataApiService.getOptionMarketData("BTC-USD", "");
		return AjaxResult.success(result);
	}

	/**
	 * 获取预估交割/行权价格 Get Estimated Delivery/Excercise Price GET
	 * /api/v5/public/estimated-price
	 */
	@GetMapping("/xingquanjiage")
	@ResponseBody
	public AjaxResult getEstimatedDeliveryExcercisePrice() {
		JSONObject result = publicDataApiService.getEstimatedDeliveryExcercisePrice("BTC-USD-210326");
		return AjaxResult.success(result);
	}

	/**
	 * 获取免息额度和币种折算率 Get Discount Rate And Interest-Free Quota GET
	 * /api/v5/public/discount-rate-interest-free-quota
	 */
	@GetMapping("rateAndFreeQuota")
	@ResponseBody
	public AjaxResult getDiscountRateAndInterestFreeQuota() {
		JSONObject result = publicDataApiService.getDiscountRateAndInterestFreeQuota(null);
		return AjaxResult.success(result);
	}

	/**
	 * 获取系统时间 Get System Time GET /api/v5/public/time
	 */
	@GetMapping("/systemTime")
	@ResponseBody
	public AjaxResult getSystemTime() {
		JSONObject result = publicDataApiService.getSystemTime();
		return AjaxResult.success(result);
	}

	/**
	 * 获取平台公共爆仓单信息 Get Liquidation Orders GET /api/v5/public/liquidation-orders
	 */
	@GetMapping("/baocang")
	@ResponseBody
	public AjaxResult getLiquidationOrders() {
		JSONObject result = publicDataApiService.getLiquidationOrders("FUTURES", null, null, null, "BTC-USDT",
				"quarter", "filled", null, null, null);
		return AjaxResult.success(result);
	}

	/**
	 * 获取标记价格 Get Mark Price GET /api/v5/public/mark-price
	 */
	@GetMapping("markPrice")
	@ResponseBody
	public AjaxResult getMarkPrice() {
		JSONObject result = publicDataApiService.getMarkPrice("FUTURES", "", "");
		return AjaxResult.success(result);
	}

	/**
	 * 获取合约衍生品仓位档位 Get Mark Price GET /api/v5/public/tier
	 */
	@GetMapping("/tier")
	@ResponseBody
	public AjaxResult getTier() {
		JSONObject result = publicDataApiService.getTier("FUTURES", "BTC-USDT", null, "isolated", null, null);
		return AjaxResult.success(result);
	}
}
